/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.XmlDataBase;

import java.util.Collection;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author Viktor
 */
public class MovieManagerImpl implements MovieManger{

    @Override
    public Element createMovie(Movie movie,Document doc) {
        if(movie == null){
            throw new NullPointerException("Movie is null");
        }
       
        try {
            Element movXml = doc.createElement("movie");
            
            Element title = doc.createElement("title");
            title.appendChild(doc.createTextNode(movie.getTitle()));
            movXml.appendChild(title);
            
            Element length = doc.createElement("length");
            length.appendChild(doc.createTextNode(movie.getLength()));
            movXml.appendChild(length);
            
            Element languages = doc.createElement("languages");
            Collection<String> langs = movie.getLanguages();
            for (String lang : langs) {
                Element language = doc.createElement("language");
                language.appendChild(doc.createTextNode(lang));
                languages.appendChild(language);
            }
            movXml.appendChild(languages);
            
            if (movie.getFormat() != null) {
                Element format = doc.createElement("format");
                format.appendChild(doc.createTextNode(movie.getFormat()));
                movXml.appendChild(format);
            }
            if (movie.getGenre() != null) {
                Element genre = doc.createElement("genre");
                genre.appendChild(doc.createTextNode(movie.getGenre()));
                movXml.appendChild(genre);
            }
            if (movie.getNote() != null) {
                Element note = doc.createElement("note");
                note.appendChild(doc.createTextNode(movie.getNote()));
                movXml.appendChild(note);
            }
            return movXml;
            
        } catch (Exception ex) {
            Logger.getLogger(MovieManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
}
